function prepareFormEvents(){
	var myTypes=['areacodeUS','email','phoneprefixUS','phonesuffixUS','telUS','url','zipcodeUS'];
	$('input,textarea').each(function(index){
		if(myTypes.indexOf($(this).data('myType'))>=0){
			$(this).click(function(){
				$(this).parent().RemoveBubblePopup();
			});
			$(this).keyup(function(){
				$(this).parent().RemoveBubblePopup();
				if(validateInput($(this))){
					$(this).css('color','');
				}else{
					$(this).css('color','#f00');
				}
			});
		}
	});
}
function validateInput(inputElement){
	var isValidInput=true;
	var pat='';
	switch(inputElement.data('myType')){
	case 'areacodeUS':
		pat=/^[2-9][0-9]{2}$/;
		break;
	case 'email':
		pat=/^([a-zA-Z0-9_\-\.]+)@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.)|(([a-zA-Z0-9\-]+\.)+))([a-zA-Z]{2,4}|[0-9]{1,3})(\]?)$/;
		break;
	case 'phoneprefixUS':
		pat=/^[0-9]{3}$/;
		break;
	case 'phonesuffixUS':
		pat=/^[0-9]{4}$/;
		break;
	case 'telUS':
		pat=/^(\([2-9][0-9]{2}\) |\([2-9][0-9]{2}\)|[2-9][0-9]{2}-|[2-9][0-9]{2})([0-9]{3}|[0-9]{3}-)[0-9]{4}$/;
		break;
	case 'url':
		pat=/^((http|https|ftp):\/\/(www\.)?|www\.)[a-zA-Z0-9\_\-]+\.([a-zA-Z]{2,4}|[a-zA-Z]{2}\.[a-zA-Z]{2})(\/[a-zA-Z0-9\-\._\?\&=,'\+%\$#~]*)*$/;
		break;
	case 'zipcodeUS':
		pat=/^\d{5}$/;
		break;
	}
	if(pat!=''){
		if(!pat.test(inputElement.val())){
			isValidInput=false;
		}
	}
	return isValidInput;
}
function validateForm(formSelector){
	var isValidForm=true;
	$(formSelector+' input,'+formSelector+' textarea').each(function(index){
		if($(this).attr('type')=='checkbox'){
			if($(this).data('myRequired')){
				if($(formSelector+' input[name="'+$(this).attr('name')+'"]:checked').length==0){
					isValidForm=false;
				}
			}
		}else{
			if($(this).data('myRequired') && !$(this).val().length>0){
				isValidForm=false;
			}
			if(isValidForm && $(this).val().length>0){
				if(!validateInput($(this))){
					isValidForm=false;
				}
			}
		}
		if(!isValidForm){
			$(formSelector).CreateBubblePopup({innerHtml:$(this).data('myMessage'),innerHtmlStyle:{'color':'#000000'},position:'bottom',themeName:'all-yellow'});
			$(formSelector).ShowBubblePopup();
			$(formSelector).FreezeBubblePopup();
			return false;
		}
	});
	return isValidForm;
};
function submitForm(formSelector){
	a=validateForm(formSelector);
	if(a){
//		$('#buttonSubmit').attr("disabled",true);
		$.post(
			'',
			$(formSelector).serialize(),
			function(data,status){
				$(formSelector).CreateBubblePopup({innerHtml:data,innerHtmlStyle:{'color':'#000000'},position:'bottom',themeName:'all-yellow'});
				$(formSelector).ShowBubblePopup();
				$(formSelector).FreezeBubblePopup();
			}
		);
	}
	return false;
};
$(window).load(function(){
	prepareFormEvents();
});

